import bpy

from ..clazz.BlenderMCPClazz import BlenderMCPServer
from ..config import __addon_name__
from ..preference.AddonPreferences import ExampleAddonPreferences


# This Example Operator will scale up the selected object
class ExampleOperator(bpy.types.Operator):
    '''ExampleAddon'''
    bl_idname = "object.example_ops"
    bl_label = "ExampleOperator"

    # 确保在操作之前备份数据，用户撤销操作时可以恢复
    bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(cls, context: bpy.types.Context):
        return context.active_object is not None

    def execute(self, context: bpy.types.Context):
        addon_prefs = bpy.context.preferences.addons[__addon_name__].preferences
        assert isinstance(addon_prefs, ExampleAddonPreferences)
        # use operator
        # bpy.ops.transform.resize(value=(2, 2, 2))

        # manipulate the scale directly
        context.active_object.scale *= addon_prefs.number
        return {'FINISHED'}


# 当前只是为了测试常驻按钮的点击测试-点击对图形缩小0.8
class StaticButtonOperator(bpy.types.Operator):
    '''常驻按钮的点击测试'''
    bl_idname = "object.blender_mcp_operator"
    bl_label = "StaticButtonOperator"

    # 确保在操作之前备份数据，用户撤销操作时可以恢复
    bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(cls, context: bpy.types.Context):
        return context.active_object is not None

    def execute(self, context: bpy.types.Context):
        addon_prefs = bpy.context.preferences.addons[__addon_name__].preferences
        assert isinstance(addon_prefs, ExampleAddonPreferences)
        # use operator
        # manipulate the scale directly
        context.active_object.scale *= 0.8
        return {'FINISHED'}


class BlenderMCPOperatorStart(bpy.types.Operator):
    '''BlenderMCPOperatorStart'''
    bl_idname = "object.blender_mcp_operator_start"
    bl_label = "Now_Stop_Click_Start"

    # 确保在操作之前备份数据，用户撤销操作时可以恢复
    bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(cls, context: bpy.types.Context):
        return context.active_object is not None

    def execute(self, context: bpy.types.Context):
        scene = context.scene

        # 创建socket实例
        if not hasattr(bpy.types, "blender_mcp_server") or not bpy.types.blender_mcp_server:
            # Start the server
            bpy.types.blender_mcp_server = BlenderMCPServer(port=9876)
            bpy.types.blender_mcp_server.start()
            print("---------------------Start MCP Server后重置状态")
            scene.blender_mcp_server_running = True
        return {'FINISHED'}


class BlenderMCPOperatorStop(bpy.types.Operator):
    '''BlenderMCPOperatorStop'''
    bl_idname = "object.blender_mcp_operator_stop"
    bl_label = "Now_Start_Click_Stop"

    @classmethod
    def poll(cls, context: bpy.types.Context):
        return context.active_object is not None

    def execute(self, context: bpy.types.Context):
        scene = context.scene

        # 销毁socket实例
        if hasattr(bpy.types, "blender_mcp_server") or bpy.types.blender_mcp_server:
            bpy.types.blender_mcp_server.stop()
            del bpy.types.blender_mcp_server
            print("---------------------Stop MCP Server后重置状态")
            scene.blender_mcp_server_running = False
        return {'FINISHED'}
